package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* WifiSignalShowView4
* */
class WifiSignalShowView4 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private var mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private var currentLevel = 2

    private val startAngle = 220F
    private val sweepAngle = 100F
    private val sweepAngle_2 = 360F

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_C70CE6)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint1_0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_16AA05)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_C70CE6)
            style = Paint.Style.FILL
        }
    }

    private val mPaint2_0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_16AA05)
            style = Paint.Style.FILL
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val space = mWidth.toFloat() / 32
            for (i in 0.until(count)) {
                val offsetY = space + (i * count) * space
                val condition = (i + 1) == count
                save()
                translate(0F, offsetY)
                drawArc(
                    offsetY,
                    offsetY,
                    mWidth.toFloat() - offsetY,
                    mHeight.toFloat() - offsetY,
                    startAngle,
                    if (condition) sweepAngle_2 else sweepAngle,
                    false,
                    if (condition) {
                        if (currentLevel < LEVEL_1) mPaint2_0 else mPaint2
                    } else {
                        if (currentLevel > levelArr[i]) mPaint1 else mPaint1_0
                    }
                )
                restore()
            }

//            save()
//            translate(0F, space)
//            drawArc(
//                space,
//                space,
//                mWidth.toFloat() - space,
//                mHeight.toFloat() - space,
//                startAngle,
//                sweepAngle,
//                false,
//                if (currentLevel > LEVEL_3) mPaint1 else mPaint1_0
//            )
//            restore()
//
//            save()
//            translate(0F, space * 5)
//            drawArc(
//                space * 5,
//                space * 5,
//                mWidth.toFloat() - space * 5,
//                mHeight.toFloat() - space * 5,
//                startAngle,
//                sweepAngle,
//                false,
//                if (currentLevel > LEVEL_3) mPaint1 else mPaint1_0
//            )
//            restore()
//
//            save()
//            translate(0F,space * 9)
//            drawArc(
//                space * 9,
//                space * 9,
//                mWidth.toFloat() - space * 9,
//                mHeight.toFloat() - space * 9,
//                startAngle,
//                sweepAngle,
//                false,
//                if (currentLevel > LEVEL_2) mPaint1 else mPaint1_0
//            )
//            restore()
//
//            save()
//            translate(0F,space * 13)
//            drawArc(
//                space * 13,
//                space * 13,
//                mWidth.toFloat() - space * 13,
//                mHeight.toFloat() - space * 13,
//                startAngle,
//                sweepAngle_2,
//                false,
//                if (currentLevel < LEVEL_1) mPaint2_0 else mPaint2
//            )
//
//            restore()

        }
    }

    companion object {
        const val LEVEL_4 = 4
        const val LEVEL_3 = 3
        const val LEVEL_2 = 2
        const val LEVEL_1 = 1

        const val count = 4

        val levelArr = intArrayOf(LEVEL_3, LEVEL_2, LEVEL_1)
    }
}